package com.sxjh.controller.ny;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxjh.common.BaseController;
import com.sxjh.common.Constant;
import com.sxjh.common.Result;
import com.sxjh.common.ZPage;
import com.sxjh.entity.OperationLog;
import com.sxjh.entity.param.OperationLogAddParam;
import com.sxjh.entity.param.OperationLogParam;
import com.sxjh.entity.result.OperationLogResult;
import com.sxjh.service.OperationLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/operation")
@Api(tags = "异常流程操作记录")
public class OperationLogController extends BaseController {
    @Autowired
    private OperationLogService operationLogService;

    @ApiOperation("列表")
    @PostMapping("/list")
    public Result<ZPage<OperationLogResult>> list(@RequestBody OperationLogParam param) {
        IPage iPage = new Page(param.getCurrent(), param.getSize());
        if (param.getSize() == -1) {
            iPage.setSize(Constant.maxSize);
        }
        iPage = operationLogService.list(iPage, param);
        ZPage<OperationLogResult> zPage = copyPage(iPage, OperationLogResult::new);
        return Result.ok(zPage);
    }

    @ApiOperation("保存记录")
    @PostMapping("/save")
    public Result save(@RequestBody OperationLogAddParam param) {
        //log.info("the interface request param :{}" + param);
        OperationLog operationLog = new OperationLog();
        BeanUtils.copyProperties(param, operationLog);
        operationLog.setCreatedAt(LocalDateTime.now());
        operationLogService.save(operationLog);
        return Result.ok("保存记录成功 id:" + operationLog.getId());
    }

}
